<?php

/* ----------------------------------------------
  Caloris: Core Member Class

  $Id$
  $Author$

  Preinstalled extension
  ---------------------------------------------- */

/* admin_toolbar: Admin toolbar */

function admin_toolbar() {
    // Check module settings
    if (defined('_MODULE_ACTIVE') && file_exists(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/module-' . _MODULE_ACTIVE . '.xml')) {
        $xmlLinkF = simplexml_load_file(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/module-' . _MODULE_ACTIVE . '.xml');
        $xmlLink = $xmlLinkF->xpath('//links');

        for ($c = 0; $c < count($xmlLink[0]->link); $c++) {
            $links .= '<li><a href="' . $xmlLink[0]->link[$c]->href . '">' . $xmlLink[0]->link[$c]->title . '</a></li>';
        }

        $content .= '
<ul class="nav nav-pills">
' . $links . '
</ul>' . PHP_EOL;
    } elseif (defined('_MODULE_ACTIVE')) {
        $xmlLinkF = simplexml_load_file(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/modules.xml');
        $xmlLink = $xmlLinkF->xpath('//module[@id="' . _MODULE_ACTIVE . '"]');

        for ($c = 0; $c < count($xmlLink[0]->links->link); $c++) {
            $links .= '<a href="' . $xmlLink[0]->links->link[$c]->href . '">' . $xmlLink[0]->links->link[$c]->title . '</a> | ';
        }

        $content .= '
<ul class="nav nav-pills">
' . substr($links, 0,-2) . '
</ul>' . PHP_EOL;
    }

    return $content;
}

/* admin_version: Admin version */

function admin_version() {
    if (!$_COOKIE["auser"] == '') {

        if (\Caloris\Time::dateDifference('yyyy-m-d h:n:s', _CALSET_CALORIS_VERSION_LAST_CHECK, date("Y-m-d H:i:s")) > 172800) {
            $ch = curl_init();

            if ($ch) {
                $ret = curl_setopt($ch, CURLOPT_URL, _CALSET_CALORIS_SETTINGS);
                $ret = curl_setopt($ch, CURLOPT_HEADER, false);
                $ret = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                $ret = curl_setopt($ch, CURLOPT_NOBODY, 0);
                $ret = curl_setopt($ch, CURLOPT_TIMEOUT, 40);

                $ret = curl_exec($ch);

                if (empty($ret)) {
                    die(curl_error($ch));
                    curl_close($ch);
                } else {
                    $info = curl_getinfo($ch);
                    curl_close($ch);

                    if (empty($info['http_code'])) {
                        $content .= 'empty';
                    } else {
                        $xmstr = simplexml_load_string($ret);
                        $file = _CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/settings.xml';
                        $xml = simplexml_load_file($file);

                        $remote = $xmstr->caloris->version_minor;
                        $local = $xml->caloris->version;

                        if (strcmp($remote, $local) != 0) {
                            $xml->asXML($file);
                            $version = '(<a href="' . _CALSET_CALORIS_HOMEPAGE . '/download/" style="color: yellow; text-decoration: underline;">' . $xmstr->caloris->version_minor . '</a>)';
                        }
                    }
                }

                // Saving the settings
                $xma = simplexml_load_file(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/settings.xml');
                $xma->caloris->version_last_check = date("Y-m-d H:i:s");
                $xma->caloris->version_latest = $xmstr->caloris->version;
                $xma->asXML(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/settings.xml');
            }
        }


        $file = _CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/settings.xml';
        $xml = simplexml_load_file($file);

        if (strcmp($xml->caloris->version, $xml->caloris->version_latest) != 0) {
            $version = '(<a href="' . _CALSET_CALORIS_HOMEPAGE . '/download/" style="color: yellow; text-decoration: underline;">' . $xml->caloris->version_latest . '</a>)';
        }


        $xmlUser = simplexml_load_file(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/users.xml');
        $xmlUserEdit = $xmlUser->xpath('//user[@id="' . $_COOKIE["auser"] . '"]');

        $content = '[cal:def(caloris;version) /] ' . $version . '';

        return $content;
    }
}

/* contacts_page: Contact Page */

function contacts_page() {
// Contact page for presentation
    $content = '';

    $xmlContact = simplexml_load_file(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/contacts.xml');
    $xmlContactList = $xmlContact->xpath('//contact[show="1"]');

    if (count($xmlContactList) > 0) {

        for ($a = 0; $a < count($xmlContactList); $a++) {
            $content .= '
<div style="margin-bottom: 10px;">

<p style="font-weight: bold; letter-spacing: 0.08em;">' . $xmlContactList[$a]->name . '</p>
<p>' . $xmlContactList[$a]->address . '</p>
<p>' . $xmlContactList[$a]->zip . ' ' . $xmlContactList[$a]->city . '</p>' . PHP_EOL;
            if ($xmlContactList[$a]->country["show"] == 1) {
                $content .= '<p>' . $xmlContactList[$a]->country . '</p>';
            }

            if (count($xmlContactList[$a]->emails->email) > 0) {
                $content .= '<p style="margin: 10px 0; font-weight: bold;">E-mail:</p>';
            }


            for ($b = 0; $b < count($xmlContactList[$a]->emails->email); $b++) {
                if ($xmlContactList[$a]->emails->email[$b]["show"] == 1) {
                    $content .= '<p class="newtext">' . $xmlContactList[$a]->emails->email[$b]["type"] . ': <a href="mailto:' . $xmlContactList[$a]->emails->email[$b] . '">' . $xmlContactList[$a]->emails->email[$b] . '</a></p>';
                }
            }

            if (count($xmlContactList[$a]->phone->number) > 0) {
                $content .= '<p style="margin: 10px 0; font-weight: bold;">Telefon:</p>';
            }

            for ($b = 0; $b < count($xmlContactList[$a]->phone->number); $b++) {
                if ($xmlContactList[$a]->phone->number[$b]["show"] == 1) {
                    $content .= '<p class="newtext">' . $xmlContactList[$a]->phone->number[$b]["type"] . ': ' . $xmlContactList[$a]->phone->number[$b] . '</p>';
                }
            }

            if ($xmlContactList[$a]->ic["show"] == 1 && $xmlContactList[$a]->ic != '') {
                $content .= '<p class="newtext">IČ: ' . $xmlContactList[$a]->ic . '</p>';
            }

            if ($xmlContactList[$a]->dic["show"] == 1 && $xmlContactList[$a]->dic != '') {
                $content .= '<p>DIČ: ' . $xmlContactList[$a]->dic . '</p>';
            }

            $content .= '
</div>
';
        }

        $content .= '
';
    } else {
        $content .= '';
    }


    return $content;
}

/* def: Output for php constants including simple formatting */

function def($name, $key) {
    $val1 = "_CALSET_" . strtoupper($name) . "_" . strtoupper($key);

    if (defined($val1)) {
        $content = constant($val1);
    }

    return $content;
}

/* ln: Link Managemenent */

function ln($name, $additional = '', $querystring = '') {

    $slash = '/';

    if (_CALSET_BASIC_MOD_REWRITE == 0 or file_exists(_CALSET_PATHS_BASE . '/.htaccess') == false) {
        if ($querystring == '') {
            if ($additional == '') {
                $content = _CALSET_PATHS_URI . $slash . 'index.php?ctemplate=' . $name;
            } else {
                $content = _CALSET_PATHS_URI . $slash . 'index.php?ctemplate=' . $name . '&amp;cat=' . $additional;
            }
        } else {
            $content = _CALSET_PATHS_URI . $slash . 'index.php?ctemplate=' . $name . '&amp;cat=' . $additional . '&amp;' . $querystring;
        }
    } else {
        if ($name == 'index') {
            $slash = '';
            $name = '';
        }

        if ($querystring == '') {
            $content = _CALSET_PATHS_URI . $slash . $name . '/' . $additional;
        } else {
            $content = _CALSET_PATHS_URI . $slash . $name . '/' . $additional . '?' . $querystring;
        }
    }

    return $content;
}

/* menu_navigator: Extension for displaying categories and subcategories in XML */

function menu_navigator($categoryID = 0, $style = '', $link = '') {
    $content = '';
    // reading the file
    $xml = simplexml_load_file(_CALSET_PATHS_BASE . '/cal-components/data/categories.xml');

    // getting identifier
    if ($categoryID == 0) {

        if ($_GET["cat"] != '') {
            $xpathCategory = '//category[short="' . $_GET["cat"] . '"]';
            $resultCategory = $xml->xpath($xpathCategory);
            $categoryID = $resultCategory[0]["id"];
        } else {
            $categoryID = $_REQUEST["id"];
        }
    }

    if ($categoryID != '') {
        $xpath = '//category[@id="' . $categoryID . '"]';
    } else {
        $xpath = '/core';
    }


    $result = $xml->xpath($xpath);

    while (list(, $node) = each($result)) {
        if ($style == '') {
            $content .= '
<h2>' . $node->name . '</h2>
';
        }

        $content = '
<ul class="' . $style . '">' . PHP_EOL;

        for ($a = 0; $a < count($node->category); $a++) {
            if ($node->category[$a]->link != '') {
                if (substr($node->category[$a]->link, 0, 7) == 'http://') {
                    $link = $node->category[$a]->link;
                } else {
                    $link = ln($node->category[$a]->link);
                }
            } else {
                $link = ln('katalog-kategorie', $node->category[$a]->short, 'type=' . Caloris\Text::protect($_GET["type"]));
            }

            $content .= '
    <li><a href="' . $link . '">' . $node->category[$a]->name . '</a></li>';

            unset($link);
        }
    }

    $content .= '
</ul>' . PHP_EOL;

    return $content;
}

/* predefs: GET, COOKIE, SERVER, SESSION, POST for a given querystring name */

function predefs($x = '') {
    if (isset($_REQUEST[$x])) {
        $content = $_REQUEST[$x];
    } else {
        $content = '';
    }

    return $content;
}

/* t: Translating extension */

function t($keyword, $source = '', $replace = '') {
    $lang = _CALSET_CALORIS_LANGUAGE;

    if ($source == '') {
        $xmlPath = _CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/lang.xml';
    } else {
        $xmlPath = _CALSET_PATHS_BASE . _CALSET_DIRS_MODULE . '/' . $source . '/lang.xml';
    }

    $xmlLanguageF = simplexml_load_file($xmlPath);
    $xmlLanguage = $xmlLanguageF->xpath('//snippets[@id="' . $keyword . '"]/' . $lang);

    if ($replace != '') {
        $content = str_replace('%T', $replace, $xmlLanguage[0]);
    } else {
        $content = $xmlLanguage[0];
    }

    return $content;
}